package com.zdsoft.blp.zongbuju.wode.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.fanhui.FanHuiView;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.zongbuju.wode.adapter.SheQuxzAdapter;
import com.zdsoft.blp.zongbuju.wode.entity.SheQu;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/2.
 */
public class WDXuanZeSheQu extends BaseActivity implements View.OnClickListener, NetCallback {
    private ListView lv;
    private SheQuxzAdapter adapter;
    private FanHuiView fh;
    private ImageView iv_sousuo;
    private TextView tv_tijiao;
    private SheQu shequs;
    private SharedPreferences sp;
    public static List<SheQu> lists = null;

    @Override
    protected void init() {
        setContentView(R.layout.wd_xuanzeshequ);
        lv = find(R.id.lv);
        fh = find(R.id.fh);
        iv_sousuo = find(R.id.iv_sousuo);
        tv_tijiao = find(R.id.tv_tijiao);


        Intent intent = getIntent();
        int type = intent.getIntExtra("type", 0);
        fh.getTv_title().setText("选择社区");
        if (type == 1) {
            tv_tijiao.setText("提交关注");
        } else if (type == 2) {
            fh.getLl_back().setVisibility(View.GONE);
            iv_sousuo.setVisibility(View.GONE);
            tv_tijiao.setText("去看看吧");
        }

        Net net = new Net();
        net.url = API.COMMUNTY_URL;
        net.nameSpace = API.COMMUNTY_NAMESPACE;
        net.methodName = API.COMMUNTY_LIST;
        Map<String, Object> map = new HashMap<>();
        map.put("pageNo", 1);
        map.put("pageSize", 10);
        map.put("condition", "");
        net.parms = map;
        net.code = 1;
        getServer(net, this);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for (int i = 0; i < lists.size(); i++) {
                    lists.get(i).setXuanZhong(false);
                }
                adapter.change(position);
            }
        });

        sp = getSharedPreferences("info", Context.MODE_PRIVATE);

        tv_tijiao.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_tijiao:
                for (int i = 0; i < lists.size(); i++) {
                    if (lists.get(i).getXuanZhong()) {
                        if (sp.getBoolean("isLoginSuccess", false)) {
                            String userId = sp.getString("userId", "");
                            if (!"".equals(userId)) {
                                Net net = new Net();
                                net.url = API.COMMUNTY_URL;
                                net.nameSpace = API.COMMUNTY_NAMESPACE;
                                net.methodName = API.SAVECOMMUNITY;
                                Map<String, Object> map = new HashMap<>();
                                map.put("userId", userId);
                                map.put("communityCode", lists.get(i).getCode());
                                net.parms = map;
                                getServer(net, this);
                            }
                        }
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("communityCode", lists.get(i).getCode());
                        editor.putString("communityName", lists.get(i).getName());
                        editor.commit();
                    }
                }
                /*Intent intent = new Intent(WDXuanZeSheQu.this, MainActivity.class);
                startActivity(intent);*/
                WDXuanZeSheQu.this.finish();
                break;
        }
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
        if (flag && code == 1) {
            lists = jiexi(res);
            adapter = new SheQuxzAdapter(getApplicationContext());
            lv.setAdapter(adapter);
        }

    }

    private List<SheQu> jiexi(String result) {
       lists = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(result);
            JSONArray array = object.getJSONArray("rows");
            for (int i = 0; i < array.length(); i++) {
                JSONObject object2 = array.getJSONObject(i);
                String firstLetter = object2.getString("firstLetter");
                String name = object2.getString("name");
                String code = object2.getString("code");
                if (code.equals(sp.getString("communityCode", ""))) {
                    shequs = new SheQu(name, firstLetter, code, true);
                } else {
                    shequs = new SheQu(name, firstLetter, code, false);
                }
                lists.add(shequs);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return lists;
    }
}
